<template>
    <div class="app-container">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <!-- <el-form-item label="设备id" prop="deviceId">
          <el-input
            v-model="queryParams.deviceId"
            placeholder="请输入设备id"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <el-form-item label="借用用户" prop="userId">
          <!-- <el-input
            v-model="queryParams.userId"
            placeholder="请输入借用用户"
            clearable
            @keyup.enter.native="handleQuery"
          /> -->
           <el-select
              v-model="queryParams.userId"
              filterable
              remote
              reserve-keyword
              placeholder="请输入关键词"
              :remote-method="queryUserAsync"
              :loading="loading_select"
            >
              <el-option
                v-for="item in UserList"
                :key="item.userId"
                :label="item.nickName + '(' + item.remark + ')'"
                :value="item.userId"
              >
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="借用时间" prop="borrowTime">
          <el-date-picker
            clearable
            v-model="queryParams.borrowTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择借用时间"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="计划归还时间" prop="returnTime">
          <el-date-picker
            clearable
            v-model="queryParams.returnTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划归还时间"
          >
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="归还状态" prop="returnStatus">
          <el-select
            v-model="queryParams.returnStatus"
            placeholder="请选择归还状态"
            clearable
          >
            <el-option
              v-for="dict in dict.type.device_return_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['device:deviceBorrow:add']"
            >快速借用</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['device:deviceBorrow:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['device:deviceBorrow:remove']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['device:deviceBorrow:export']"
            >导出</el-button
          >
        </el-col> -->
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
  
      <el-table
        height="480"
        class="borrowTable"
        v-loading="loading"
        :data="deviceBorrowList"
        @selection-change="handleSelectionChange"
        border=""
      >
        <el-table-column type="selection" width="55" align="center"  />
        <el-table-column label="借用人工/学号" align="center" prop="sysUser.userName" />
         <el-table-column label="用户名称" align="center" prop="sysUser.nickName"/>
        <el-table-column label="设备名称" align="center" prop="deviceName" />
        <el-table-column label="设备型号" align="center" prop="deviceModel" />
        <el-table-column label="设备照片" align="center" prop="deviceImg" width="100">
         <template slot-scope="scope">
            <image-preview :src="scope.row.deviceImg" :width="50" :height="50"/>
          </template>
      </el-table-column>
       <el-table-column label="借用说明" align="center" prop="borrowRemark" />
        <el-table-column
          label="借用时间"
          align="center"
          prop="borrowTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.borrowTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        
        <el-table-column
          label="计划归还时间"
          align="center"
          prop="returnTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.returnTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="归还说明" align="center" prop="returnRemark" />
        <el-table-column label="状态" align="center" prop="returnStatus">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.device_return_status"
              :value="scope.row.returnStatus"
            />
          </template>
        </el-table-column>
          <el-table-column label="设备状态" align="center" prop="deviceStatus">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.device_info_status" :value="scope.row.deviceStatus"/>
          </template>
        </el-table-column>
        <!-- <el-table-column label="创建人id" align="center" prop="createBy" />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新人id" align="center" prop="updateBy" />
        <el-table-column
          label="更新时间"
          align="center"
          prop="updateTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column> -->
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['device:deviceBorrow:edit']"
              >快速归还</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['device:deviceBorrow:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改设备借用对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="设备" prop="deviceId">
            <!-- <el-input v-model="form.deviceId" placeholder="请输入设备名称" /> -->
            <el-select
              v-model="form.deviceId"
              filterable
              remote
              reserve-keyword
              placeholder="请输入关键词"
              :remote-method="queryDeviceAsync"
              :loading="loading_select"
            >
              <el-option
                v-for="item in deviceInfoList"
                :key="item.typeId"
                :label="item.typeName"
                :value="item.typeId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="借用用户" prop="userId">
            <!-- <el-input v-model="form.userId" placeholder="请输入借用用户id" /> -->
            <el-select
              v-model="form.userId"
              filterable
              remote
              reserve-keyword
              placeholder="请输入关键词"
              :remote-method="queryUserAsync"
              :loading="loading_select"
            >
              <el-option
                v-for="item in UserList"
                :key="item.userId"
                :label="item.nickName + '(' + item.remark + ')'"
                :value="item.userId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="借用时间" prop="borrowTime" v-show="loading_dev1">
            <el-date-picker
              clearable
              v-model="form.borrowTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择借用时间"
              :disabled="form.borrowId != null"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="借用说明" prop="borrowRemark" v-show="loading_dev1">
            <el-input v-model="form.borrowRemark" placeholder="请输入借用说明" :disabled="form.borrowId != null"/>
          </el-form-item>
          <el-form-item label="计划归还时间" prop="returnTime" v-show="loading_dev1">
            <el-date-picker
              clearable
              v-model="form.returnTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择计划归还时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="归还状态" prop="returnStatus" v-show="false">
            <el-radio-group v-model="form.returnStatus">
              <el-radio
                v-for="dict in dict.type.device_return_status"
                :key="dict.value"
                :label="parseInt(dict.value)"
                >{{ dict.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <el-form-item label="设备状态" prop="deviceStatus" v-show="loading_dev2">
            <el-select v-model="form.deviceStatus" placeholder="请选择设备状态">
              <el-option
                v-for="dict in dict.type.device_info_status"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="归还说明" prop="returnRemark" v-show="loading_dev2">
            <el-input v-model="form.returnRemark" placeholder="请输入归还说明" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import {listDeviceBorrow,getDeviceBorrow,delDeviceBorrow,addDeviceBorrow,updateDeviceBorrow,queryUser,queryDevice} from "@/api/device/deviceBorrow";
  import { listTable } from "@/api/device/deviceInfo";
  export default {
    name: "DeviceBorrow",
    dicts: ["device_return_status","device_info_status"],
    data() {
      return {
        // 遮罩层
        loading: true,
        loading_select: true,
        loading_dev1 :false,
        loading_dev2 :false,
        dis:false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 设备借用表格数据
        deviceBorrowList: [],
        // 设备信息查询表格数据
        deviceInfoList: [],
        // 用户表格数据
        UserList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          deviceId: null,
          userId: null,
          borrowTime: null,
          returnTime: null,
          returnStatus: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          deviceId: [
            { required: true, message: "设备名称不能为空", trigger: "blur" },
          ],
          userId: [
            { required: true, message: "借用用户id不能为空", trigger: "blur" },
          ],
          borrowTime: [
            { required: true, message: "借用时间不能为空", trigger: "blur" },
          ],
          // returnStatus: [
          //   { required: true, message: "归还状态不能为空", trigger: "change" },
          // ],
        },
      };
    },
    created() {
      this.getDevice();
    },
    methods: {
      /** 查询设备借用列表 */
      getList() {
        this.loading = true;
        listDeviceBorrow(this.queryParams).then((response) => {
          this.deviceBorrowList = response.rows;
          // 遍历设备借用列表
          for (let i = 0; i < this.deviceBorrowList.length; i++) {
            // 遍历设备列表
            for (let j = 0; j < this.deviceInfoList.length; j++) {
              //判断设备借用信息和设备信息的deviceId是否相同，然后将deviceName赋值给设备借用的deviceName
              if (
                this.deviceBorrowList[i].deviceId ==this.deviceInfoList[j].deviceId
              ) {
                this.deviceBorrowList[i].deviceName =this.deviceInfoList[j].deviceName;
                this.deviceBorrowList[i].deviceImg =this.deviceInfoList[j].deviceImg;
                this.deviceBorrowList[i].deviceModel =this.deviceInfoList[j].deviceModel;
                this.deviceBorrowList[i].deviceStatus =this.deviceInfoList[j].deviceStatus;
                break;
              }
            }
          }
  
          this.total = response.total;
          this.loading = false;
        });
      },
      getDevice() {
        this.loading = true;
        listTable(this.queryParams).then((response) => {
          this.deviceInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
          this.getList();
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.loading_dev1=false;
        this.loading_dev2=false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          borrowId: null,
          deviceId: null,
          userId: null,
          borrowTime: null,
          borrowRemark: null,
          returnTime: null,
          returnStatus: null,
          returnRemark: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.borrowId);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.dis=false;
        this.loading_dev1=true;
        this.title = "设备借用";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const borrowId = row.borrowId || this.ids;
        getDeviceBorrow(borrowId).then((response) => {
          this.deviceInfoList = [{
            typeName: row.deviceName,
            typeId: row.deviceId,
            deviceStatus:row.deviceStatus
          }];
          this.UserList = [ {
             nickName:row.sysUser.nickName,
             userId:row.sysUser.userId,
             remark:row.sysUser.remark,
          }];
          
          this.loading_dev2=true;
          this.loading_dev1=true;
          this.dis=true;
          // this.form = response.data;
           this.form = {...row}
  
          this.open = true;
          this.title = "设备归还";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.borrowId != null) {
              this.form.deviceInfo = {
                deviceStatus: this.form.deviceStatus,
                deviceName: this.form.deviceName,
                deviceId: this.form.deviceId
              }
              updateDeviceBorrow(this.form).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getDevice();
              });
            } else {
              addDeviceBorrow(this.form).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getDevice();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const borrowIds = row.borrowId || this.ids;
        this.$modal
          .confirm('是否确认删除设备借用编号为"' + borrowIds + '"的数据项？')
          .then(function () {
            return delDeviceBorrow(borrowIds);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      },
      //用户
      queryUserAsync(queryString) {
        this.loading_select = true;
        if (queryString == "") {
          // this.UserList=[];
          this.loading_select = false;
        } else {
          queryUser(queryString).then((res) => {
            if (res.code == 200) {
              this.UserList = res.data;
            }
            this.loading_select = false;
          });
        }
      },
       //设备
      queryDeviceAsync(queryString) {
        this.loading_select = true;
        if (queryString == "") {
          this.loading_select = false;
        } else {
          queryDevice(queryString).then((res) => {
            if (res.code == 200) {
              this.deviceInfoList = res.data;
            }
            this.loading_select = false;
          });
        }
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(
          "device/deviceBorrow/export",
          {
            ...this.queryParams,
          },
          `deviceBorrow_${new Date().getTime()}.xlsx`
        );
      },
    },
  };
  </script>
  